using System.Diagnostics;

namespace Box {
    public class ProcessNodeTask : ProcessNode,IUpdateNode {
        public IProcessTreeTask Task;
        public bool IsLock = false;
        public bool PowerActivate = false;
        public bool PowerLock = false;

        public override (string,ProcessTreeNodePortType)[] GetPortsDefines() {
            return new (string, ProcessTreeNodePortType)[]{
                ("Input",ProcessTreeNodePortType.Input),
                ("Output",ProcessTreeNodePortType.Output),

                ("Activate",ProcessTreeNodePortType.Input),
                ("Lock",ProcessTreeNodePortType.Input),

                ("Executing",ProcessTreeNodePortType.Output),
                ("Complete",ProcessTreeNodePortType.Output),
            };
        }

        public override void onInputHigh(string from_port) {
            if(!IsLock) {
                switch(from_port) {
                    case "Input" : {
                        Output("Output",ProcessTreeNodePortStatus.High);
                    }break;
                    case "Activate" : {
                        if(!PowerActivate) {
                            if(Task.Status == ProcessTreeTaskStatus.Complete) {
                                Output("Complete",ProcessTreeNodePortStatus.Low);
                            }

                            PowerActivate = true;
                            Task.Status = ProcessTreeTaskStatus.Activate;
                            Task.onActivate();
                            Output("Executing",ProcessTreeNodePortStatus.High);
                        } 
                        
                    }break;
                    case "Lock" : {
                        if(!PowerLock) {
                            PowerLock = true;
                            IsLock = true;
                        } 
                    }break;
                }
            } else {
                if(from_port == "Input") {
                    Output("Output",ProcessTreeNodePortStatus.High);
                }
            }
        }
        public override void onInputLow(string from_port){
            Output("Output",ProcessTreeNodePortStatus.Low);
            if(!IsLock) {
                switch(from_port) {
                    case "Input" : {
                        Output("Output",ProcessTreeNodePortStatus.Low);
                    }break;
                    case "Activate" : {
                        PowerActivate = false;
                        Task.Status = ProcessTreeTaskStatus.Stop;
                        Task.onStop();
                    }break;
                    case "Lock" : {
                        PowerLock = false;
                        IsLock = false;
                    }break;
                }
            } else {
                switch(from_port) {
                    case "Input" : {
                        Output("Output",ProcessTreeNodePortStatus.Low);
                    }break;
                    case "Lock" : {
                        PowerLock = false;
                        IsLock = false; 
                    }break;
                }
            }
        }

        public void onUpdate(double delta)
        {
            if(Task.Status == ProcessTreeTaskStatus.Activate) {
                if(Task.Execute(delta)) {
                    Task.Status = ProcessTreeTaskStatus.Complete;
                    Task.onComplete();
                    Output("Executing",ProcessTreeNodePortStatus.Low);
                    Output("Complete",ProcessTreeNodePortStatus.High);
                }
            }
        }
    }
}